package com.htdu87.vnlicenceplatedetection;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

public class CamPreview extends SurfaceView implements SurfaceHolder.Callback {
	private Camera mCamera;
	private SurfaceHolder mHolder;
	private final String TAG = "htdu87 debug";
	private Size previewSize;
	
	public CamPreview(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings("deprecation")
	public CamPreview(Context context, Camera camera) {
		super(context);
		// TODO Auto-generated constructor stub
		mCamera = camera;
		mHolder = getHolder();
		mHolder.addCallback(this);
		// deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        Point screenSize = new Point();
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		display.getSize(screenSize);
        
        FrameLayout layPreview = (FrameLayout)((Activity)context).findViewById(R.id.lay_preview);
        Parameters pars = mCamera.getParameters();
        
        List<Size> lstPreviewSupportSize = pars.getSupportedPreviewSizes();
        for(int i = lstPreviewSupportSize.size() - 1; i >= 0; i--){
        	Size size = lstPreviewSupportSize.get(i);
        	if(size.width >= screenSize.x || i == 0){
        		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(size.width, size.height);
        		lp.setMargins(Math.abs((size.width - screenSize.x))/2, 0, 0, 0);
        		layPreview.setLayoutParams(lp);
        		previewSize = size;
        		Log.d(TAG, "Setted preview size: "+size.width+"x"+size.height);
        		break;
        	}
        }
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		try {
			mCamera.setPreviewDisplay(mHolder);
			mCamera.startPreview();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mCamera.stopPreview();
		mCamera.release();
		Log.d(TAG, "Camera has been release");
	}

	public Size getPreviewSize() {
		return previewSize;
	}
}
